package ee.roosideSoda.backend.model.dao.postgresqlimplementations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import ee.roosideSoda.backend.database.ConnectionManager;
import ee.roosideSoda.backend.model.dao.YldkysimusegaSeotudTiimidDAO;
import ee.roosideSoda.backend.model.data.YldkysimusegaSeotudTiim;

public class YldkysimusegaSeotudTiimidDAOPostgresqlImpl implements YldkysimusegaSeotudTiimidDAO
{
	Logger logger = Logger.getLogger(YldkysimusegaSeotudTiimidDAOPostgresqlImpl.class);
	Connection dbConnection;
	String sql;
	PreparedStatement preparedStatement;
	Statement statement;
	ResultSet resultSet;
	private final int WARNING_NO_ROWS_AFFECTED = 0;
	private int rowsAffected;
	
	public YldkysimusegaSeotudTiimidDAOPostgresqlImpl(Connection dbConnection)
	{
		this.dbConnection = dbConnection;
	}

	@Override
	public int lisaYldkysimusegaSeotudTiim(
			YldkysimusegaSeotudTiim yldkysimusegaSeotudTiim) 
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "INSERT INTO YldkysimusegaSeotudTiimid " + 
				"(yldkysimusFK, tiimFK, valestiVastamisteArv) " + 
				"VALUES (?, ?, ?)";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
			
			preparedStatement.setInt	(1, yldkysimusegaSeotudTiim.getYldkysimusFK());
			preparedStatement.setInt	(2, yldkysimusegaSeotudTiim.getTiimFK());
			preparedStatement.setInt 	(3, yldkysimusegaSeotudTiim.getValestiVastamisteArv());
			
			logger.info(preparedStatement.toString());
			rowsAffected = preparedStatement.executeUpdate();
			
			if (rowsAffected == 1)
			{
				return rowsAffected;
			}
			else
			{
				System.out.println("No rows affected!");
				logger.info("Unsuccessful insertion to a database: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (Exception e) 
		{
			e.printStackTrace();
			return WARNING_NO_ROWS_AFFECTED;
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}

	@Override
	public List<YldkysimusegaSeotudTiim> saaYldkysimusgaSeotudTiimideList(
			int yldkysimusID) 
	{
		resultSet = null;
		YldkysimusegaSeotudTiim yldkysimusegaSeotudTiimiAndmedAndmebaasist = null;
		List<YldkysimusegaSeotudTiim> yldkysimusgaSeotudTiimideList = new ArrayList<YldkysimusegaSeotudTiim>();
		sql = "SELECT * FROM YldkysimusegaSeotudTiimid WHERE yldkysimusFK = ? " +
				"ORDER BY tiimFK";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(
						sql,
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_READ_ONLY
					);
			//filling the placeholders 
			preparedStatement.setInt(1, yldkysimusID);
			logger.info(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			//place the cursor at the beginning of a received data set (so it can start to read and process data)
			resultSet.beforeFirst();
			
			while (resultSet.next())
			{
				yldkysimusegaSeotudTiimiAndmedAndmebaasist = new YldkysimusegaSeotudTiim();
				yldkysimusegaSeotudTiimiAndmedAndmebaasist.setYldkysimusFK(resultSet.getInt("yldkysimusFK"));
				yldkysimusegaSeotudTiimiAndmedAndmebaasist.setTiimFK(resultSet.getInt("tiimFK"));
				yldkysimusegaSeotudTiimiAndmedAndmebaasist.setValestiVastamisteArv(resultSet.getInt("valestiVastamisteArv"));
				
				yldkysimusgaSeotudTiimideList.add(yldkysimusegaSeotudTiimiAndmedAndmebaasist);
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return yldkysimusgaSeotudTiimideList;
	}

	@Override
	public int suurendaTiimiValestiVastamisteArvu(int yldkysimusID, int tiimID) 
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "UPDATE YldkysimusegaSeotudTiimid " + 
				"SET valestiVastamisteArv = valestiVastamisteArv + 1 " + 
				"WHERE yldkysimusFK = ? AND tiimFK = ? AND valestiVastamisteArv BETWEEN 0 AND 2"; 
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql);
			
			preparedStatement.setInt	(1, yldkysimusID);
			preparedStatement.setInt	(2, tiimID);
			
			rowsAffected = preparedStatement.executeUpdate();
			
			
			if (rowsAffected == 1)
			{
				logger.info("Update sucessful: " + preparedStatement.toString());
				return rowsAffected;
			}
			else
			{
				logger.error("Update was unsuccesful: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
			return WARNING_NO_ROWS_AFFECTED;		
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}
	
	@Override
	public int saaTiimiValestiVastamisteArv(int yldkysimusID, int tiimID)
	{
		resultSet = null;
		int valestiVastamisteArv = -1;
		sql = "SELECT valestiVastamisteArv FROM YldkysimusegaSeotudTiimid " +
				"WHERE yldkysimusFK = ? AND tiimFK = ?";
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(
						sql,
						ResultSet.TYPE_SCROLL_INSENSITIVE,
						ResultSet.CONCUR_READ_ONLY
					);
			//filling the placeholders 
			preparedStatement.setInt(1, yldkysimusID);
			preparedStatement.setInt(2, tiimID);
			logger.info(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			//place the cursor at the beginning of a received data set (so it can start to read and process data)
			resultSet.beforeFirst();
			
			if (resultSet.next())
			{
				valestiVastamisteArv = (resultSet.getInt("valestiVastamisteArv"));
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
		
		return valestiVastamisteArv;
	}
	
	@Override
	public int vahendaTiimiValestiVastamisteArvu(int yldkysimusID, int tiimID) 
	{
		resultSet = null;
		rowsAffected = WARNING_NO_ROWS_AFFECTED;
		sql = "UPDATE YldkysimusegaSeotudTiimid " + 
				"SET valestiVastamisteArv = valestiVastamisteArv - 1 " + 
				"WHERE yldkysimusFK = ? AND tiimFK = ? AND valestiVastamisteArv BETWEEN 1 AND 3"; 
		
		try 
		{
			preparedStatement = dbConnection.prepareStatement(sql);
			
			preparedStatement.setInt	(1, yldkysimusID);
			preparedStatement.setInt	(2, tiimID);
			
			rowsAffected = preparedStatement.executeUpdate();
			
			
			if (rowsAffected == 1)
			{
				logger.info("Update sucessful: " + preparedStatement.toString());
				return rowsAffected;
			}
			else
			{
				logger.error("Update was unsuccesful: " + preparedStatement.toString());
				return WARNING_NO_ROWS_AFFECTED;
			}
		}
		catch (SQLException e) 
		{
			logger.error(e.getMessage());
			return WARNING_NO_ROWS_AFFECTED;		
		}
		finally
		{
			ConnectionManager.getInstance().closeStatement(preparedStatement);
			ConnectionManager.getInstance().closeResultSet(resultSet);
		}
	}
}
